package root.electronclass.electronclass.home.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.os.BundleKt;
import androidx.recyclerview.widget.GridLayoutManager;


import root.electronclass.electronclass.common.base.BasicAdapter;
import root.electronclass.electronclass.common.base.BindingHolder;
import root.electronclass.electronclass.mvp.GlobalParameter;
import root.electronclass.electronclass.mvp.base.BaseResponse;
import root.electronclass.electronclass.mvp.entity.HomeHonorInfoH;
import root.electronclass.electronclass.mvp.rest.RestManager;
import root.electronclass.electronclass.CommonActivity;
import root.electronclass.electronclass.MainActivity;
import root.electronclass.electronclass.databinding.HomeHonorInfoBinding;
import root.electronclass.electronclass.databinding.HomeItemHonorBinding;
import root.electronclass.electronclass.grade.honor.ClassHonorDetailFragment;
import root.electronclass.electronclass.grade.honor.ClassHonorFragment;
import root.electronclass.electronclass.grade.model.ClassHonor;
import root.electronclass.electronclass.grade.model.MediaVo;


import java.util.List;

import kotlin.Pair;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeHonorView {
    private static final String TAG = "HomeHonorView";

    private Context context;
    private HomeHonorInfoBinding binding;

    public HomeHonorView(Context context, ViewGroup view) {
        this.binding = HomeHonorInfoBinding.bind(view);
        this.context = context;
        init();
    }

    private BasicAdapter<HomeHonorInfoH, HomeItemHonorBinding> adapter = new BasicAdapter<HomeHonorInfoH, HomeItemHonorBinding>() {
        @Override
        public void onBindViewHolder(@NonNull BindingHolder<HomeItemHonorBinding> holder, @SuppressLint("RecyclerView") HomeHonorInfoH item) {
            holder.getBinding().tvHonorName.setText(item.getTitle());
            holder.getBinding().tvHonorDate.setText(item.getDate());
//            holder.getBinding().tvHonorType.setText(item.getType() == 0 ? "个人" : "集体");
            if(item != null && item.getPicList() != null && item.getPicList().size() > 0) {
                holder.getBinding().imgCertificate.setImageURI(item.getPicList().get(0).getUrl());
            }

            holder.getBinding().getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, CommonActivity.class);
                    MediaVo[] list;
                    if (item.getPicList() != null && item.getPicList().size() > 0) {
                        list = new MediaVo[item.getPicList().size()];
                    } else {
                        list = new MediaVo[0];
                    }
                    ClassHonor classHonor = new ClassHonor(
                            item.getId(),
                            item.getPicList().toArray(list),
                            item.getTitle(),
                            item.getType(),
                            item.getName(),
                            item.getDate(),
                            item.getCompany(),
                            item.getCreateTime(),
                            item.getReason(),
                            item.getUserList(),
                            item.getUserType());
                    intent.putExtras(BundleKt.bundleOf(new Pair<>("_DATA", classHonor)));
                    intent.putExtra("_FRAGMENT_CLASS", ClassHonorDetailFragment.class);
                    context.startActivity(intent);
                }
            });
        }

        @Override
        public void onBindViewHolder(@NonNull BindingHolder<HomeItemHonorBinding> holder, int position) {
            super.onBindViewHolder(holder, position);


        }
    };

    private void init() {
        binding.rvHonorList.setVisibility(View.GONE);
        binding.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity fragmentActivity = ((MainActivity)context);
                Bundle bundle = new Bundle();
                bundle.putSerializable("TARGET_CLASS", ClassHonorFragment.class);
                fragmentActivity.selectPage(MainActivity.Page.CLASS, bundle);
            }
        });
        request();
    }
    public void refresh() {
        request();
    }

    public void request() {
        long id = GlobalParameter.getEcardInfo().getClassId();
        RestManager.getInstance().initRest().homeHonor(String.valueOf(id)).enqueue(new Callback<BaseResponse<List<HomeHonorInfoH>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<HomeHonorInfoH>>> call, Response<BaseResponse<List<HomeHonorInfoH>>> response) {
                if (response.isSuccessful()) {
                    binding.rvHonorList.setVisibility(View.VISIBLE);

                    adapter.replaceAll(response.body().getRows());
                    binding.rvHonorList.setAdapter(adapter);
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<List<HomeHonorInfoH>>> call, Throwable t) {

            }
        });
    }


}
